<?php 
require 'header.php';   
?>
<?php
session_start();
if ($_GET['action'] == 'add') {
include("connect.php");
include("cbooks.php");
$exname=strtolower(substr($_FILES['file']['name'],(strrpos($_FILES['file']['name'],'.')+1)));
$uploadfile = getname($exname);
move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);
if(trim($_FILES['file']['name']!=""))
 {
  $uploadfile=$uploadfile;
}
else
 {
  $uploadfile="";
 }
$bookname=$_POST['bookname'];
$article=$_POST['article'];
$classify=$_POST['classify'];
$pic=$uploadfile;
$rtime=date("Y/m/d h:i");
$user=$_SESSION['username'];
$sql="insert into bookinfo (id,bookname,file,article,username,rtime,classify) values (null,'".$bookname."','".$pic."','".$article."','".$user."','".$rtime."','".$classify."')";
if(mysqli_query($conn,$sql)){
	echo "<script>alert('添加成功！');window.location='books.php';</script>";
}else{
	echo "<script>alert('操作失败!');window.location='SubmitBook.php';</script>";
}
	
}
?>
<?php
		if($_SESSION['username']!=null) {
			?>
			<div class="container" style="margin-top: 30px;">
				<div class="card border-primary mb-3">
				  <div class="card-header">上传书籍</div>
				  <div class="card-body text-primary">
				    <h5 class="card-title">好好的分享哦！</h5>
				    <form  method="post" enctype="multipart/form-data" name="login" action="SubmitBook.php?action=add">
				    <div class="form-group">
				    	<label for="exampleFormControlInput1">书籍名称：</label>
				    	<input type="text" class="form-control" name="bookname" autocomplete="off" placeholder="请输入书籍名称" required="required" id="exampleFormControlInput1">
				    </div>
				    <div class="form-group">
				    	<label for="exampleFormControlInput1">文件：</label>
				    	<input  type="file" required="required" name="file" id="file">
				    </div>
				    <div class="form-group">
				    	<label for="exampleFormControlSelect1">书籍分类</label>
				    	<select class="form-control" name="classify" id="exampleFormControlSelect1">
				    	<option>未分类</option>
				    	<option>历史</option>
				    	<option>社会科学</option>
				    	<option>计算机</option>
				    	<option>音乐</option>
						<option>生物</option>
						<option>其他</option>
				    	</select>
				    </div>
				    <div class="form-group">
				    	<label for="exampleFormControlTextarea1">详细介绍</label>
				    	<textarea name="article" required="required" class="form-control"  placeholder="请输入介绍信息" ></textarea>
				    </div>
				    <div class="form-group">
				    	<input type="submit" value="提交" class="btn btn-primary" /> 
				    </div>
				    </form>
				  </div>
				</div>				
	</div>
			<?php
		}else{
			?>
			请登录
			<?php
		}
	?>
	</body>
